﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cxx.Common;
using Cxx.Entity;
using Cxx.Entity.Extend;
using Cxx.IDAL;
using Dapper;

namespace Cxx.DAL
{
    public class StockRecordDal: DataAccess, IStockRecordDal
    {
        public void Insert(StockRecordInfo info)
        {
            string sql =
                "INSERT INTO tblStockRecord (Receiver,TradeName,StockType,OrderCode,CreateTime,TotalCount,StockCode) VALUES(@Receiver,@TradeName,@StockType,@OrderCode,@CreateTime,@TotalCount,@StockCode)";
            using (IDbConnection connection = GetConnection())
            {
                connection.Execute(sql, info);
            }
        }

        public List<StockRecordInfo> Get(string recevier)
        {
            object para = new {Receiver = recevier};
            string sql = "SELECT * FROM tblStockRecord WHERE Receiver=@Receiver";
            using (IDbConnection connection = GetConnection())
            {
                return connection.Query<StockRecordInfo>(sql, para)?.ToList();
            }
        }

        public List<StockRecordInfo> GetList(string condition)
        {
            #region 获取查询条件
            Hashtable fields = new Hashtable();
            var field = new DataFieldInfo("Receiver", "Character", "tblStockRecord", "LIKE");
            fields.Add("txtReceiver", field);
            field = new DataFieldInfo("CreateTime", "DateTime", "tblStockRecord", ">=");
            fields.Add("txtStartTime", field);
            field = new DataFieldInfo("CreateTime", "DateTime", "tblStockRecord", "<=");
            fields.Add("txtEndTime", field);
            field = new DataFieldInfo("OrderCode", "Character", "tblStockRecord", "=");
            fields.Add("txtOrderCode", field);
            field = new DataFieldInfo("StockType", "Integer", "tblStockRecord", "=");
            fields.Add("ddlStockType", field);
            #endregion

            string sqlWhere = SqlFilter.Build(condition, fields);
            string sql = "SELECT * FROM tblStockRecord";
            if (!string.IsNullOrWhiteSpace(sqlWhere))
            {
                sql += " WHERE " + sqlWhere;
            }
            using (IDbConnection dbConnection = GetConnection())
            {
                return dbConnection.Query<StockRecordInfo>(sql)?.ToList();
            }
        }

        public List<StockRecordInfo> GetListForPage(string condition, int pageIndex, int pageSize, out int recordCount)
        {
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("@_tables", "tblStockRecord", DbType.String, null, 1000);
            //parameters.Add("@PK", "Id", DbType.String, null, 100);
            parameters.Add("@_orderby", "Id DESC", DbType.String, null, 200);
            parameters.Add("@_pageindex", pageIndex, DbType.Int32);
            parameters.Add("@_pageSize", pageSize, DbType.Int32);
            parameters.Add("@_fields", "*", DbType.String, null, 1000);

            #region 获取查询条件
            Hashtable fields = new Hashtable();
            var field = new DataFieldInfo("Receiver", "Character", "tblStockRecord", "LIKE");
            fields.Add("txtReceiver", field);
            field = new DataFieldInfo("CreateTime", "DateTime", "tblStockRecord", ">=");
            fields.Add("txtStartTime", field);
            field = new DataFieldInfo("CreateTime", "DateTime", "tblStockRecord", "<=");
            fields.Add("txtEndTime", field);
            field = new DataFieldInfo("OrderCode", "Character", "tblStockRecord", "=");
            fields.Add("txtOrderCode", field);
            field = new DataFieldInfo("StockType", "Integer", "tblStockRecord", "=");
            fields.Add("ddlStockType", field);

            #endregion
            string sqlWhere = SqlFilter.Build(condition, fields);

            parameters.Add("@_where", sqlWhere, DbType.String, null, 1000);
            parameters.Add("@_totalcount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            parameters.Add("@_pagecount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            using (IDbConnection dbConnection = GetConnection())
            {
                var result = dbConnection.Query<StockRecordInfo>(USP_PAGING_PROCEDURE, parameters,
                    commandType: CommandType.StoredProcedure);
                if (result == null)
                {
                    recordCount = 0;
                    return null;
                }
                recordCount = parameters.Get<int>("@_totalcount");
                return result.ToList();
            }
        }
    }
}
